﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using KC.ExtensionMethods;

namespace KC.Business.Text
{
	public class Encoding
	{
		public static string BlogEncodeForStorage(string input)
		{
			if (string.IsNullOrEmpty(input)) throw new ArgumentNullException("input");
            input = input
                .Trim()
                .Replace("[[", "<")
                .Replace("]]", ">")
                .Replace("<br />", "\r\n");
			while (input.Contains("\r\n\r\n\r\n")) input = input.Replace("\r\n\r\n\r\n", "\r\n\r\n");
			return input;
		}
		public static string BlogDecodeForRendering(string input)
		{
			if (string.IsNullOrEmpty(input)) throw new ArgumentNullException("input");
			string output = input
				// Base formatting and replacements
                .Replace("\r\n", "<br />\n")
				.Replace("[[", "<")
				.Replace("]]", ">")
				.Replace("</li>\r\n", "</li>")
				.Replace("<ul>\r\n ", "<ul>")
				.Replace("</ul>\r\n", "</ul>")
				.Trim()
				.Replace("<ol>", "\r\n<ol>")						//
				.Replace("<ul>", "\r\n<ul>")						//	Source formatting for readability
				.Replace("</ol>", "\r\n</ol>\r\n")					//
				.Replace("</ul>", "\r\n</ul>\r\n")					//
				.Replace("<a href=", "<a target=\"_blank\" href=")	// Ensure all hyperlinks go to a new window


				// Redirect all links and image requests to KConnolly.NET
				.Replace("href=\"http://www.damnednice.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://products.damnednice.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://blog.damnednice.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://damnednice.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://www.zi255.com", "href=\"http://www.kconnolly.net")
				.Replace("href=\"http://zi255.com", "href=\"http://www.kconnolly.net")
				.Replace("src=\"http://www.damnednice.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://products.damnednice.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://blog.damnednice.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://damnednice.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://www.zi255.com", "src=\"http://www.kconnolly.net")
				.Replace("src=\"http://zi255.com", "src=\"http://www.kconnolly.net")

				// Oops
				.Replace("Verizon Business", "A Major Telecom")
				.Replace("Verizon", "A Major Telecom")
				// .Replace("<br /><br />", "<br />")
				.Replace("!notruncate", "")

				// Postformatting
				.Replace("<li>", "\r\n <li>")
				.Replace("</li>", "</li>\r\n")
				.Replace("</li>\r\n<br />", "</li>\r\n")
				.Replace("\n \r\n ", "\r\n")
				.Replace("</ul><br />\n", "</ul>\r\n")
				.Replace("</ul>\r\n<br />\n<br />\n", "</ul>\r\n\r\n")
				.Trim();
			//while (output.Contains("<br />\n<br />\n<br />\n"))
			//    output = output.Replace("<br />\n<br />\n", "<br />\n");

            output = output .Replace("<br />\n\n", "<br /><br />");

            while (output.Contains("<br /><br /><br />"))
                output = output.Replace("<br /><br /><br />", "<br /><br />");

			return output;
		}
		/// <summary>Given a blob of text, gets a simplified and stripped-down version of it.</summary>
		/// <param name="body">Body of text to format</param>
		/// <returns>Short excerpt from the beginning of the text</returns>
		public static string ExtractExcerpt(string body)
		{
			if (string.IsNullOrEmpty(body)) throw new ArgumentNullException("body");

			int start = 0, stop = 0;
			body = Regex.Replace(
				body
					.ToLower()
					.Replace("!notruncate", "")
					.Trim()
					.Replace("[[", "<")
					.Replace("]]", ">")
					.Replace("<br>", " ")
					.Replace("<br />", " "),
				@"<.*?>", "");
			while (body.Contains("  ")) body = body.Replace("  ", " ");
			if (body.Length > 255) body = body.Substring(0, 255);

			// Set excerpt positions
			start = 0;
			stop = 500;
			if (stop > body.Length) stop = body.Length;

			string Excerpt = body.Substring(start, stop - start);

			// Adjust stop position to be outside HTML tags
			while (stop < body.Length && !Excerpt.EndsWithAny(new string[] { "<", ">", "[[", "]]" })) {
				stop++;
				Excerpt = body.Substring(start, stop - start);
				if (Excerpt.EndsWith(">")) {
					stop++;
					Excerpt = body.Substring(start, stop - start);
					break; }
				if (Excerpt.EndsWith("]]")) {
					stop += 2;
					Excerpt = body.Substring(start, stop - start);
					break; }
				if (Excerpt.EndsWithAny(new string[] { "<", "[[" })) {
					Excerpt = Excerpt.Substring(0, Excerpt.Length - 1);
					break; } }
			
			Excerpt = Excerpt.Replace("<b>...</b>", "");
			return Excerpt;
		}

	}
}
